package com.example.ice.u17;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.ice.u17.fragment.BookshelfFragment;
import com.example.ice.u17.fragment.BoutiqueFragment;
import com.example.ice.u17.fragment.GameFragment;
import com.example.ice.u17.fragment.RecommendFragment;
import com.example.ice.u17.global.U17Application;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    private RadioButton mRbRecommend;
    private RadioButton mRbBoutique;
    private RadioButton mRbBookshelf;
    private RadioButton mRbGame;
    private RadioGroup mRg;
    private FragmentManager mFragmentManager;
    private Fragment currentFragment;
    private RecommendFragment recommendFragment;
    private BoutiqueFragment boutiqueFragment;
    private BookshelfFragment bookshelfFragment;
    private GameFragment gameFragment;
    private FloatingActionButton fab;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private int fabTop;
    private int iv1H;
    private int iv2H;
    private int iv3H;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        U17Application.context = this;
        initView();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private void initView() {
        mRbRecommend = (RadioButton) findViewById(R.id.rb_recommend);
        mRbRecommend.setOnClickListener(this);
        mRbBoutique = (RadioButton) findViewById(R.id.rb_boutique);
        mRbBoutique.setOnClickListener(this);
        mRbBookshelf = (RadioButton) findViewById(R.id.rb_bookshelf);
        mRbBookshelf.setOnClickListener(this);
        mRbGame = (RadioButton) findViewById(R.id.rb_game);
        mRbGame.setOnClickListener(this);
        mRg = (RadioGroup) findViewById(R.id.rg);
        mRg.setOnClickListener(this);
        mFragmentManager = getSupportFragmentManager();
        recommendFragment = new RecommendFragment();
        boutiqueFragment = new BoutiqueFragment();
        bookshelfFragment = new BookshelfFragment();
        gameFragment = new GameFragment();
        currentFragment = recommendFragment;
        mFragmentManager.beginTransaction().add(R.id.container, recommendFragment).commit();
        mFragmentManager.beginTransaction().add(R.id.container, boutiqueFragment).hide(boutiqueFragment).commit();
        mFragmentManager.beginTransaction().add(R.id.container, bookshelfFragment).hide(bookshelfFragment).commit();
        mFragmentManager.beginTransaction().add(R.id.container, gameFragment).hide(gameFragment).commit();
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(this);
        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        fabTop = fab.getTop();
        iv1H = iv1.getHeight();
        iv2H = iv2.getHeight();
        iv3H = iv3.getHeight();
    }

    boolean isOpen = false;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_recommend:
                mFragmentManager.beginTransaction().hide(currentFragment).show(recommendFragment).commit();
                currentFragment = recommendFragment;
                break;
            case R.id.rb_boutique:
                mFragmentManager.beginTransaction().hide(currentFragment).show(boutiqueFragment).commit();
                currentFragment = boutiqueFragment;
                break;
            case R.id.rb_bookshelf:
                mFragmentManager.beginTransaction().hide(currentFragment).show(bookshelfFragment).commit();
                currentFragment = bookshelfFragment;
                break;
            case R.id.rb_game:
                mFragmentManager.beginTransaction().hide(currentFragment).show(gameFragment).commit();
                currentFragment = gameFragment;
                break;
            case R.id.fab:
                if (isOpen) {
                    closeAnimation();
                } else {
                    openAnimation();
                }
                break;
            case R.id.iv1:
                mFragmentManager.beginTransaction().hide(currentFragment).show(recommendFragment).commit();
                currentFragment = recommendFragment;
                check(0);
                closeAnimation();
                break;
            case R.id.iv2:
                mFragmentManager.beginTransaction().hide(currentFragment).show(boutiqueFragment).commit();
                currentFragment = boutiqueFragment;
                check(1);
                closeAnimation();
                break;
            case R.id.iv3:
                mFragmentManager.beginTransaction().hide(currentFragment).show(gameFragment).commit();
                currentFragment = gameFragment;
                check(3);
                closeAnimation();
                break;
        }
    }

    private void check(int position) {
        for (int i = 0; i < mRg.getChildCount(); i++) {
            RadioButton childAt = (RadioButton) mRg.getChildAt(i);
            if (position != i) {
                childAt.setChecked(false);
            } else {
                childAt.setChecked(true);
            }
        }
    }

    public void openAnimation() {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 135, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setFillAfter(true);
        fab.startAnimation(rotateAnimation);

        setImgsVis(View.VISIBLE);
        ViewCompat.animate(iv1).setDuration(1000).alpha(1).translationYBy((-iv1H - 10)).start();
        ViewCompat.animate(iv2).setDuration(1000).alpha(1).translationYBy((-iv1H - iv2H - 20)).start();
        ViewCompat.animate(iv3).setDuration(1000).alpha(1).translationYBy((-iv1H - iv2H - iv3H - 30)).start();
        isOpen = true;
    }

    public void setImgsVis(int visible) {
        iv1.setVisibility(visible);
        iv2.setVisibility(visible);
        iv3.setVisibility(visible);
    }

    public void closeAnimation() {
        RotateAnimation rotateAnimation = new RotateAnimation(135, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setFillAfter(true);
        fab.startAnimation(rotateAnimation);

        ViewCompat.animate(iv1).setDuration(1000).alpha(0).translationYBy((iv1H + 10)).start();
        ViewCompat.animate(iv2).setDuration(1000).alpha(0).translationYBy((iv1H + iv2H + 20)).start();
        ViewCompat.animate(iv3).setDuration(1000).alpha(0).translationYBy((iv1H + iv2H + iv3H + 30)).start();
        isOpen = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setImgsVis(View.GONE);
                    }
                });
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (U17Application.context != null) {
            U17Application.context = null;
        }
    }
}
